package com.mhuang.module.wechat.config;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.mhuang.module.wechat.service.WechatConfigService;
import com.mhuang.wechat.common.config.WeConfig;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

/**
 * 
 * @Package: com.mhuang.module.wechat.config
 * @Description 微信配置
 * @author huang.miao
 * @date 2017年2月6日 下午5:15:49  
 * @since 1.0.0
 * @group skiper-opensource
 */
@Component
@Data
public class WechatConfig {

	@Autowired
	private WechatConfigService wechatConfigService;
	
	@Value("#{wechatConfigProperties.WECHAT_SERVER_TYPE}")
	private String wechat_server_type;
	
	@Value("#{wechatConfigProperties.WECHAT_TOKEN_REFRESHTIME}")
	private String wechat_token_refreshTime;
	
	@Value("#{wechatConfigProperties.WECHAT_ACCESS_TOKEN_URL}")
	private String wechat_access_token_url;
	
	@Value("#{wechatConfigProperties.WECHAT_CREATE_MENU_URL}")
	private String wechat_create_menu_url;
	
	@Value("#{wechatConfigProperties.WECHAT_GETUSER_URL}")
	private String wechat_getuser_url;
	
	@Value("#{wechatConfigProperties.WECHAT_OAUTH_GETCODE_URL}")
	private String wechat_oauth_getcode_url;
	
	@Value("#{wechatConfigProperties.WECHAT_OAUTH_URL}")
	private String wechat_oauth_url;
	
	@Value("#{wechatConfigProperties.WECHAT_JSAPI_TOKEN_URL}")
	private String wechat_jstoken_url;
	
	@Value("#{wechatConfigProperties.WECHAT_DOWNLOAD_MEDIA_URL}")
	private String wechat_download_media_url;
	
	@Value("#{wechatConfigProperties.WECHAT_QRCODE_CREATE_URL}")
	private String wechat_qrcode_create_url;
	
	@Value("#{wechatConfigProperties.WECHAT_QRCODE_SHOW_URL}")
	private String wechat_qrcode_show_url;
	
	@Value("#{wechatConfigProperties.WECHAT_UPLOAD_MEDIA_URL}")
	private String wechat_upload_media_url;
	
	@Value("#{wechatConfigProperties.WECHAT_TEMPLATE_SEND}")
	private String wechat_template_send;
	
	public static  Long CONFIG_ID;
	
	public static enum Config{
		serverToken("serverToken","tokenRefreshTime"),
		webTicket("jsTicket","ticketRefreshTime");
		
		@Setter
		@Getter
		private String tokenType;
		
		@Setter
		@Getter
		private String refreshTime;

		public static String getValue(String name){
			for(Config config :Config.values()){
				if(StringUtils.equals(config.getTokenType(),name)){
					return config.getRefreshTime();
				}
			}
			return null;
		}
		
		Config(String tokenType,String refreshTime){
			this.tokenType = tokenType;
			this.refreshTime = refreshTime;
		}
	}
	
	public void init(){
		Map<String, Object> configMap = wechatConfigService.queryConfig();
		if(!CollectionUtils.isEmpty(configMap)){
			WeConfig.CONFIGID = (Long)configMap.get("id");
			WeConfig.APPID = (String)configMap.get("appId");
			WeConfig.API_KEY=(String)configMap.get("payKey");
			WeConfig.APPSECRET=(String)configMap.get("appSecret");
			WeConfig.MCH_ID=(String)configMap.get("mchId");
			WeConfig.TOKEN=(String)configMap.get("token");
			if(StringUtils.isNotBlank(WeConfig.APPID) &&
					StringUtils.isNotBlank(WeConfig.API_KEY) &&
					StringUtils.isNotBlank(wechat_access_token_url)){//如果不能获取access_token也就没有以下的功能
				wechat_access_token_url = wechat_access_token_url.replace("APPID", WeConfig.APPID).replace("APPSECRET", WeConfig.APPSECRET);
				wechat_oauth_getcode_url = wechat_oauth_getcode_url.replace("APPID", WeConfig.APPID);
				wechat_oauth_url = wechat_oauth_url.replace("APPID", WeConfig.APPID).replace("SECRET", WeConfig.APPSECRET);
			}
		}
	}
}
